/*
 * @Date         : 2024-05-20 15:45:40 星期1
 * @Author       : xut
 * @Description  :
 */
import { Prisma, Post } from "@prisma/client"
import { prisma } from "../utils/prisma"

export async function createPost(input: Prisma.PostCreateInput) {
  return (await prisma.post.create({ data: input })) as Post
}

export async function findPost(
  where: Partial<Prisma.PostWhereInput>,
  select?: Prisma.PostSelect
) {
  return (await prisma.post.findFirst({ where, select })) as Post
}

export async function findUniquePost(
  where: Prisma.PostWhereUniqueInput,
  select?: Prisma.PostSelect
) {
  return (await prisma.post.findUnique({ where, select })) as Post
}

export async function findAllPosts({
  page_size,
  page_no,
  select,
}: {
  page_size: number
  page_no: number
  select?: Prisma.PostSelect
}) {
  const take = page_size || 10
  const skip = (page_no - 1) * take

  return (await prisma.post.findMany({ select, skip, take })) as Post[]
}

export async function updatePost(
  where: Prisma.PostWhereUniqueInput,
  data: Prisma.PostUpdateInput,
  select?: Prisma.PostSelect
) {
  return (await prisma.post.update({ where, data, select })) as Post
}

export async function deletePost(where: Prisma.PostWhereUniqueInput) {
  return await prisma.post.delete({ where })
}
